<template>
  <van-swipe class="swipe" v-if="order.banners">
    <van-swipe-item v-for="banner in order.banners" :key="banner.url">
      <van-image height="280" fit="contain" :show-loading="false" :src="`${API_BASE_URL}${banner.url}`" />
    </van-swipe-item>
  </van-swipe>

  <div class="main-wrap">
    <div class="scroll-box">
      <transition name="van-slide-left">
        <div v-show="loaded" class="x-card card1" @click="navPage('sch')">
          <div class="x-card-item">
            <div class="theme-text">会议日程</div>
            <div class="flex-sb">
              <span class="theme-grey">SCHEDULE</span>
              <span class="nav-button">点击进入</span>
            </div>
            <van-image width="50px" class="smile-image" src="https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241220/c5c1e_smile.png" :show-loading="false" />
          </div>
        </div>
      </transition>

      <transition name="van-slide-right">
        <div class="flex-sb w100 mb10" v-show="loaded">
          <div class="x-card-twin-item mr5 card4" @click="navPage('seat')">
            <div class="theme-text">座位指引</div>
            <div class="theme-grey">SEAT</div>
            <div class="theme-grey">GUIDE</div>
            <div class="nav-button">点击进入</div>
            <van-image width="50px" class="smile-image" src="https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241220/c5c1e_smile.png" :show-loading="false" />
          </div>
          <div class="x-card-twin-item ml5 card3" @click="openLocation()">
            <div class="theme-text">交通指引</div>
            <div class="theme-grey">TRAFFIC</div>
            <div class="theme-grey">GUIDE</div>
            <div class="nav-button">点击进入</div>
            <van-image width="50px" class="smile-image" src="https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241220/c5c1e_smile.png" :show-loading="false" />
          </div>
        </div>
      </transition>

      <transition name="van-slide-left">
        <div class="flex-sb w100 mb10" v-show="loaded">
          <div class="x-card-twin-item mr5 card5" @click="navPage('note')">
            <div class="theme-text">会议事宜</div>
            <div class="theme-grey">NOTE</div>
            <div class="theme-grey">CODE</div>
            <div class="nav-button">点击进入</div>
            <van-image width="50px" class="smile-image" src="https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241220/c5c1e_smile.png" :show-loading="false" />
          </div>
          <div class="x-card-twin-item ml5 card2 disable" @click="navLive">
            <div class="theme-text">图文直播</div>
            <div class="theme-grey">LIVE</div>
            <div class="theme-grey">BROADCAST</div>
            <div class="nav-button">点击进入</div>
            <van-image width="50px" class="smile-image" src="https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241220/c5c1e_smile.png" :show-loading="false" />
          </div>
        </div>
      </transition>
    </div>
  </div>
</template>

<script setup lang="ts">
import { type BanquetOrder, type WxJsApi } from "@/types/banquet";
import request from "@/utils/http";
import { onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import wx from "weixin-js-sdk";
const API_BASE_URL = import.meta.env.VITE_API_BASE_URL || "";
import { useAppStore } from "@/stores";
import { showToast } from "vant";
const router = useRouter();
const loaded = ref(false);
const wxready = ref(false);

// 订单信息
const order = ref<BanquetOrder>({} as BanquetOrder);

const navPage = (path: string) => {
  router.push({ path });
};

const navLive = () => {
  showToast("敬请期待");
  // window.location.href = "https://m.alltuu.com/album/3140353259/1524545702?menu=live";
};

/**
 * 加载微信JS API签名
 */
const loadWxJs = async () => {
  if (!import.meta.env.PROD) {
    return;
  }
  try {
    const link = window.location.href.split("#")[0];
    const { data } = await request<WxJsApi>({
      url: `/api/wx/jsapi?url=${link}`,
      method: "GET",
    });

    if (!data) {
      console.log("wxJsApi is null");
      return;
    }

    const { nonceStr, signature, timestamp, appId } = data;

    wx.config({
      debug: false,
      appId,
      timestamp,
      nonceStr,
      signature,
      jsApiList: ["openLocation"],
    });

    wx.ready(function () {
      console.log("微信 SDK 初始化完成");
      wxready.value = true;
    });

    wx.error(function (res) {
      console.log("微信 SDK 初始化失败", res);
    });
  } catch (error) {
    console.error("加载微信配置失败:", error);
  }
};

/**
 * 打开微信地图
 */
const openLocation = () => {
  try {
    console.log("wxready", wxready.value);
    if (wxready.value === false) {
      return;
    }

    if (typeof wx === "undefined") {
      throw new Error("微信 JS-SDK 未加载");
    }

    if (typeof wx.checkJsApi !== "function") {
      throw new Error("checkJsApi 方法不可用");
    }

    wx.checkJsApi({
      jsApiList: ["openLocation"],
      success: function (res) {
        if (res.checkResult.openLocation === false) {
          console.log("不支持 openLocation API");
          return;
        }

        const { locationName, locationAddress, locationLatitude, locationLongitude } = order.value;
        wx.openLocation({
          latitude: locationLatitude,
          longitude: locationLongitude,
          name: locationName,
          address: locationAddress,
          scale: 13,
          infoUrl: "",
          success: (res) => {
            console.log("openLocation success", res);
          },
          fail: (err) => {
            console.log("openLocation fail", err);
          },
        });
      },
      fail: function (e) {
        console.log("checkJsApi fail", e);
      },
    });
  } catch (error) {
    console.error("openLocation 执行出错:", error);
  }
};

onMounted(async () => {
  const store = useAppStore();
  order.value = store.getOrder;
  await loadWxJs();
  loaded.value = true;
});
</script>

<style lang="less" scoped>
.swipe {
  margin: 10px;
}

.main-wrap {
  height: 100%;
  display: flex;
  flex-direction: column;
  box-sizing: border-box;
}

.scroll-box {
  flex: 1; /* 占满剩余高度 */
  overflow-y: auto; /* 超出时滚动 */
  -webkit-overflow-scrolling: touch; /* iOS 顺滑 */
  padding: 0 10px; /* 把左右边距从 main-wrap 移到这里，避免滚动条贴边 */
}

.theme-text {
  color: var(--ui-THEME);
  font-weight: bold;
  font-size: 20px;
  line-height: 26px;
}

.theme-grey {
  color: var(--ui-color-grey);
  font-size: 20px;
  line-height: 26px;
}

.x-card {
  display: flex;
  justify-content: center;
  align-items: center;
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  margin-bottom: 10px;
  width: 100%;
  background-size: cover;
  box-shadow: 10px 10px 30px rgba(65, 65, 65, 0.3);

  .x-card-item {
    padding: 20px 10px 20px 20px;
    width: 100%;
  }
}

.x-card-twin-item {
  position: relative;
  border-radius: 15px;
  overflow: hidden;
  padding: 20px 20px;
  width: 100%;
  background-size: cover;
  box-shadow: 10px 10px 30px rgba(65, 65, 65, 0.3);

  .nav-button {
    position: absolute;
    bottom: 10px;
    right: 10px;
  }

  &.disable::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.7);
    border-radius: inherit;
    cursor: not-allowed;
  }
}

.card1 {
  margin-top: 10px;
  background-image: url("https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241224/19edb_tiny-sch.png");
}

.card2 {
  background-image: url("https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241224/68765_tiny-live.png");
}

.card3 {
  background-image: url("https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241224/4a21a_tiny-traffice.png");
}

.card4 {
  background-image: url("https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241224/1115c_tiny-seat.png");
}

.card5 {
  background-image: url("https://owner-1256935398.cos.ap-guangzhou.myqcloud.com/prod/UPLOAD_IMAGE/20241224/d7e00_tiny-dress.png");
}

.smile-image {
  margin-top: 10px;
  opacity: 0.2;
}

.nav-button {
  padding: 4px 10px;
  background-color: var(--ui-THEME);
  font-size: 12px;
  color: #ffffff;
  border-radius: 15px;
  text-align: center;
  border: 2px solid #ffffff;
  font-weight: bold;
  box-shadow: 3px 5px 8px rgba(0, 0, 0, 0.51);
}
</style>
